package com.test.qpbgboot.backend.service.impl;

import com.test.qpbgboot.backend.mapper.MerchantMapper;
import com.test.qpbgboot.backend.pojo.dto.MerchantQuery;
import com.test.qpbgboot.backend.pojo.dto.MerchantSaveParam;
import com.test.qpbgboot.backend.pojo.entity.Merchant;
import com.test.qpbgboot.backend.pojo.vo.MerchantVO;
import com.test.qpbgboot.backend.service.MerchantService;
import com.test.qpbgboot.base.enums.EnumController;
import com.test.qpbgboot.base.enums.GeneralStatusEnum;
import com.test.qpbgboot.base.enums.MerchantStatusEnum;
import com.test.qpbgboot.base.exception.ServiceException;
import com.test.qpbgboot.base.response.ResultStatus;
import com.test.qpbgboot.show.mapper.ShopMapper;
import com.test.qpbgboot.show.pojo.dto.ShopQuery;
import com.test.qpbgboot.show.pojo.vo.MerchantShopVO;
import com.test.qpbgboot.show.pojo.vo.ShopVO;
import com.test.qpbgboot.show.service.ShopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class MerchantServiceImpl implements MerchantService {

    @Autowired
    private MerchantMapper merchantMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private ShopService shopService;

    @Override
    public List<MerchantVO> merchantSelect(MerchantQuery merchantQuery) {
        log.debug("商家查询业务，参数:{}", merchantQuery);
        List<MerchantVO> merchantVOList = merchantMapper
                .selectMerchantVObyMerchantQuery(merchantQuery);
        if (merchantVOList != null && !merchantVOList.isEmpty()) {
            for (MerchantVO merchantVO : merchantVOList) {
                additionalField(merchantVO);
            }
        }
        log.debug("商家查询结果:{}", merchantVOList);
        return merchantVOList;
    }

    @Override
    public void merchantSave(MerchantSaveParam merchantSaveParam) {
        log.debug("商家保存业务，参数:{}", merchantSaveParam);
        EnumController.checkContain(merchantSaveParam.getStatus(),"MerchantStatus");
        Merchant merchant = new Merchant();
        BeanUtils.copyProperties(merchantSaveParam, merchant);
        if (merchant.getId() == null) {
            merchantMapper.insert(merchant);
        } else {
            merchantMapper.update(merchant);
        }
    }

    @Override
    public void merchantLogOff(Long merchantId) {
        log.debug("商家注销业务，参数:{}", merchantId);

        MerchantQuery merchantQuery = new MerchantQuery();
        merchantQuery.setId(merchantId);
        List<MerchantVO> merchantVOList = merchantMapper
                .selectMerchantVObyMerchantQuery(merchantQuery);
        if (merchantVOList == null || merchantVOList.isEmpty()) {
            throw new ServiceException(ResultStatus.INVALID_ID,"无此商家");
        }
        if (merchantVOList.get(0).getStatus()
                .equals(MerchantStatusEnum.DISABLE.getValue())) {
            return;
        }

        ShopQuery shopQuery = new ShopQuery();
        shopQuery.setMerchantId(merchantId);
        shopQuery.setStatus(GeneralStatusEnum.ENABLE.getValue());
        List<ShopVO> shopVOList =
                shopMapper.selectShopVObyShopsQuery(shopQuery);
        if (shopVOList != null && !shopVOList.isEmpty()) {
            for (ShopVO shopVO : shopVOList) {
                shopService.shopLogOff(shopVO.getId());
            }
        }

        Merchant merchant = new Merchant();
        merchant.setId(merchantId);
        merchant.setStatus(MerchantStatusEnum.DISABLE.getValue());
        merchantMapper.changeStatus(merchant);
    }

    /**
     * 补充额外属性
     */
    private void additionalField(MerchantVO merchantVO) {
        if (merchantVO == null) {
            return;
        }
        List<MerchantShopVO> shopList =
                shopMapper.selectMerchantShopVObyMerchantId(
                        merchantVO.getId());
        merchantVO.setShopList(shopList);
    }
}
